package com.it.jdbc;

import com.it.jdbc.utils.JDBCUtil;
import com.it.jdbc.utils.JDBCUtils;
import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 查询语句
 */
public class JdbcQueryDemo02 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try{
            //1.注册驱动
            //DriverManager.registerDriver(new Driver());
            //2.获取连接
            //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql04","root","root");
             connection = JDBCUtils.getConnection();
            //3.编写sql
            String sql = "select * from account";
            preparedStatement = connection.prepareStatement(sql);
            //执行sql
            ResultSet resultSet = preparedStatement.executeQuery();
            //循环结果
            while(resultSet.next()){
                //id
                int id = resultSet.getInt("id");
                //name
                String name = resultSet.getString("name");
                //money
                double money = resultSet.getDouble("money");

                System.out.println(id+","+name+","+money);
            }
            //5.打印结果
            preparedStatement.close();
            //6.关闭资源
            connection.close();
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
